import React, { useImperativeHandle, forwardRef, useRef } from 'react'
import { Form, Input, Row, Col, DatePicker, Upload, Button, message } from 'antd'
import { UploadOutlined } from '@ant-design/icons'
import SubjectSelector from './SubjectSelector'
import { getUploadPropsFn, awaitWrap } from '@js/tool'
import style from '../Modal.module.less'
import { $get } from '@js/authRequest'
const { Item } = Form
const { TextArea } = Input
const shortLabel = { span: 6 }
const shortWrap = { span: 16 }

// 获取请求内容
export async function getContentRequest (data) {
  const res = await $get('/api/xapc/deliver/getDeliverContent', data)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取结束日期
export async function getDayRequest (begin, days) {
  const res = await $get('/api/common/days/getEndDatetime', {
    begin_datetime: begin,
    days
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取送达内容
async function getContent (formRef, pState) {
  const c = formRef.current
  const data = c.getFieldsValue()
  const [e, d] = await awaitWrap(getContentRequest({
    action_subject: data.action_subject,
    topic_type: pState.theme.type,
    subject_type: data.subject_type
  }))
  if (e) {
    return message.error(e.message)
  }
  formRef.current.setFieldsValue({ content: d })
}

// 改变仲裁主体回调
function onChangeContractId (o, pState, pDispatch, formRef) {
  const currentAddressList = pState.addressList.filter(li => li.contractid === o.id && li.status === 1).map((li, i) => {
    li.value = (i + 1) + '、' + li.address
    li.label = `(${i + 1})、${li.address || '无地址'}`
    return li
  })
  pDispatch({ type: 'currentAddressList', data: currentAddressList })
  getContent(formRef, pState)
}

// formRef, caseDetail, data, theme, addressList, contractList, setContractList, currentAddressList, setCurrentAddressList
function Main ({ formRef, pState, pDispatch }, ref) {
  const subjectRef = useRef(null)
  useImperativeHandle(ref, () => ({
    changeSubject: (type, formRef, caseDetail, data, dispatch) => {
      subjectRef.current.changeSubject(type, formRef, caseDetail, data, dispatch)
    }
  }))
  return (
    <>
      <Item name='status' noStyle><span /></Item>
      <Item name='contract_type' noStyle><span /></Item>
      <Item name='id' noStyle><span /></Item>
      <SubjectSelector ref={subjectRef} formRef={formRef} pState={pState} pDispatch={pDispatch} onChangeContractId={o => onChangeContractId(o, pState, pDispatch, formRef)} />
      <Item name='publication_media' label='公告媒体'>
        <Input placeholder='请输入' />
      </Item>
      <Row>
        <Col span={12}>
          <Item name='send_datetime' label='刊登日期' labelCol={shortLabel} wrapperCol={shortWrap} rules={[{ type: 'object', required: true, message: '请选择刊登日期' }]}>
            <DatePicker
              placeholder='请选择刊登日期' style={{ width: '100%' }}
            />
          </Item>
        </Col>
      </Row>
      <Item name='content' label='送达内容' rules={[{ required: true, message: '请输入送达内容' }]}>
        <TextArea rows={3} placeholder='请输入送达内容' />
      </Item>
      <Item name='file' label='送达情况材料'>
        <Upload {...getUploadPropsFn.call(this, formRef, '.jpg,.jpeg,.png', pState.fileList, v => pDispatch({ type: 'fileList', data: v }))}>
          <Button>
            <UploadOutlined /> 上传文件
          </Button>
          <span className={style['upload-placeholder']}>支持扩展名：.png .jpg</span>
        </Upload>
      </Item>
      <Item name='remark' label='备注'>
        <TextArea rows={3} placeholder='请输入备注' />
      </Item>
    </>
  )
}

export default forwardRef(Main)
